﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Coding4Fun.Kinect.Wpf;
using Microsoft.Research.Kinect.Nui;

namespace Hugo
{
    class KinectCursorController
    {
        private Runtime nui;

        public void moveUp()
        {
            if (nui != null)
            {
                int target = nui.NuiCamera.ElevationAngle + 5;
                nui.NuiCamera.ElevationAngle = Math.Min(target,50);
            }
        }

        public void moveDown()
        {
            if (nui != null)
            {
                int target = nui.NuiCamera.ElevationAngle - 5;
                nui.NuiCamera.ElevationAngle = Math.Max(target, -50);
            }
        }

        private HandCursor Cursor;

        void nui_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
        {
            SkeletonFrame allSkeletons = e.SkeletonFrame;

            if (allSkeletons == null) { return; }

            SkeletonData skeleton = (from s in allSkeletons.Skeletons
                                     where s.TrackingState == SkeletonTrackingState.Tracked
                                     select s).FirstOrDefault();

            if (skeleton == null) {
                return; 
            }
            
            Joint j = skeleton.Joints[JointID.HandRight];
            if (!GlobalSettings.isRightHanded) {
                j = skeleton.Joints[JointID.HandLeft];
            }

            double width = System.Windows.SystemParameters.PrimaryScreenWidth;
            double height = System.Windows.SystemParameters.PrimaryScreenHeight;
            var scaledJoint = j.ScaleTo((int)width, (int)height, .3f, .5f);

            this.Cursor.setCursorPosition(scaledJoint.Position.X, scaledJoint.Position.Y);
        }

        private static KinectCursorController instance;

        public static KinectCursorController getInstance() {
            return instance;
        }

        public KinectCursorController(HandCursor cursor) {

            instance = this;

            this.Cursor = cursor;

            nui = Runtime.Kinects[0];

            //initialize the kinect to perform skeletal tracking
            nui.Initialize(RuntimeOptions.UseSkeletalTracking);

            //register for SkeletonFrameReadyEvent that is triggered when skeletal data is received
            nui.SkeletonFrameReady += new EventHandler<SkeletonFrameReadyEventArgs>(nui_SkeletonFrameReady);

            //nui.NuiCamera.ElevationAngle = 20;

            //smooth out skeletal data
            nui.SkeletonEngine.TransformSmooth = true;
            TransformSmoothParameters parameters = new TransformSmoothParameters();
            parameters.Smoothing = 0.6f;
            parameters.Correction = 0.3f;
            parameters.Prediction = 0.4f;
            parameters.JitterRadius = 1.0f;
            parameters.MaxDeviationRadius = 0.5f;

            //adjust camera angle here
            nui.NuiCamera.ElevationAngle = 20;

            nui.SkeletonEngine.SmoothParameters = parameters;

        }
    }
}
